package com.qxbb.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qxbb.report.Enum.ChainRoleType;
import com.qxbb.report.Enum.LoginUserType;
import com.qxbb.report.bo.LoginUser;
import com.qxbb.report.dao.ChainSalesOrderMapper;
import com.qxbb.report.dao.ChainUserInforMapper;
import com.qxbb.report.dao.UserInforMapper;
import com.qxbb.report.pojo.ChainUserInfor;
import com.qxbb.report.pojo.UserInfo;
import com.qxbb.report.service.ChainUserInforService;
import com.qxbb.report.utils.StringUtils;
import com.qxbb.report.vo.SelectClerkVO;

import com.qxbb.report.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @version 1.0
 * @author: pebble
 * @data 2023/9/29 15:38
 */
@Service
public class ChainUserInforServiceImpl extends ServiceImpl<ChainUserInforMapper, ChainUserInfor> implements ChainUserInforService {

    @Autowired
    private ChainUserInforMapper chainUserInforMapper;
    @Autowired
    private ChainSalesOrderMapper chainSalesOrderMapper;
    @Autowired
    private UserInforMapper userInforMapper;

    @Override
    public UserInfoVO selectChainUser(LoginUser loginUser) {
        if (StringUtils.isNotNull(loginUser) && loginUser.getIsChain() == LoginUserType.chain_store.getNumber()) {
            //连锁店用户
            return chainUserInforMapper.selectChainUser(loginUser.getUserName(), loginUser.getPassword());
        } else if (StringUtils.isNotNull(loginUser) && loginUser.getIsChain() == LoginUserType.HEADQUARTERS.getNumber()) {
            //总部用户
            UserInfoVO userInfoVO = userInforMapper.selectUserInfo(loginUser.getUserName(), loginUser.getPassword());
            return userInfoVO;
        }
        return null;
    }

    @Override
    public List<SelectClerkVO> selectClerk(String chainId) {
        if (StringUtils.isNotNull(chainId)) {
            List<SelectClerkVO> list = chainUserInforMapper.selectClerk(chainId);
            return list;
        }
        return null;
    }

    @Override
    public List<SelectClerkVO> selectClerkAll() {
        return chainUserInforMapper.selectClerkAll();
    }

    @Override
    public Integer selectUserIsAdmin(Integer userId, Integer userType) {
        if (StringUtils.isNotNull(userId) && StringUtils.isNotNull(userType)) {
            //总部用户( 系统管理员 || 督导）
            if (userType == LoginUserType.HEADQUARTERS.getNumber()) {
                // 这里是总部用户 userInfo
                UserInfo userInfo = userInforMapper.selectByUserId(userId);
                if (StringUtils.isNotNull(userInfo)) {
                    if (userInfo.getRoleType() == ChainRoleType.SYSTEM_ADMIN.getRoleType() ||
                            userInfo.getRoleType() == ChainRoleType.HEADQUARTERS_SUPERVISION.getRoleType() ||
                                userInfo.getRoleType() == ChainRoleType.HEADQUARTERS_COMMODITY_MANAGER.getRoleType()) {
                        return 0;
                    }
                }
            } else {
                // 判断是否是连锁店自营者
                ChainUserInfor chainUserInfor = chainUserInforMapper.selectByUserId(userId);
                if (StringUtils.isNotNull(chainUserInfor)) {
                    if (chainUserInfor.getRoleType() == ChainRoleType.CHAIN_STORE_OPERATOR.getRoleType()) {
                        return 1;
                    }
                }
            }

        }
        return 2;
    }

    @Override
    public Boolean selectUserName(String userName,Integer userType) {
        if (userType == LoginUserType.HEADQUARTERS.getNumber()){
            UserInfo userInfo = userInforMapper.selectUserByName(userName);
            if (StringUtils.isNotNull(userInfo)){
                return true;
            }
            return false;
        } else if (userType == LoginUserType.chain_store.getNumber()) {
            ChainUserInfor userInfo = chainUserInforMapper.selectUserByName(userName);
            if (StringUtils.isNotNull(userInfo)){
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public UserInfoVO selectUser(LoginUser loginUser) {
        UserInfoVO user = null;
        if (loginUser.getIsChain().equals(LoginUserType.HEADQUARTERS.getNumber())){
            user = userInforMapper.selectUserInfo(loginUser.getUserName(),loginUser.getPassword());
            return user;
        } else if (loginUser.getIsChain().equals(LoginUserType.chain_store.getNumber())) {
            user = chainUserInforMapper.selectChainUser(loginUser.getUserName(),loginUser.getPassword());
            return user;
        }
        return null;
    }

    @Override
    public String selectUserRole(Integer userId, Integer userType) {
        //总部用户( 系统管理员 || 督导）
        if (userType == LoginUserType.HEADQUARTERS.getNumber()) {
            // 这里是总部用户 userInfo
            UserInfo userInfo = userInforMapper.selectByUserId(userId);
            if (StringUtils.isNotNull(userInfo)) {
                if (userInfo.getRoleType() == ChainRoleType.SYSTEM_ADMIN.getRoleType()) {
                    return  ChainRoleType.SYSTEM_ADMIN.getMsg();
                }else  if (userInfo.getRoleType() == ChainRoleType.HEADQUARTERS_SUPERVISION.getRoleType()){
                    return  ChainRoleType.HEADQUARTERS_SUPERVISION.getMsg();
                }
            }
        } else {
            // 判断是否是连锁店自营者
            ChainUserInfor chainUserInfor = chainUserInforMapper.selectByUserId(userId);
            if (StringUtils.isNotNull(chainUserInfor)) {
                if (chainUserInfor.getRoleType() == ChainRoleType.CHAIN_STORE_OPERATOR.getRoleType()) {
                    return ChainRoleType.CHAIN_STORE_OPERATOR.getMsg();
                }else if (chainUserInfor.getRoleType() == ChainRoleType.CHAIN_STORE_MANAGER.getRoleType()){
                    return ChainRoleType.CHAIN_STORE_MANAGER.getMsg();
                }else if (chainUserInfor.getRoleType() == ChainRoleType.CHAIN_STORE_EMPLOYEES.getRoleType()){
                    return ChainRoleType.CHAIN_STORE_EMPLOYEES.getMsg();
                }
            }
        }
        return "登录用户";
    }
}
